package com.zhitong.ztblog.controllers;


import com.zhitong.ztblog.entity.User;
import com.zhitong.ztblog.services.IUserService;
import com.zhitong.ztblog.utils.AjaxResponse;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
public class UserContro {
    @Autowired
    IUserService userService;

    @PostMapping("/login")
    public AjaxResponse login(@RequestBody User user){
        try {
            return userService.login(user);
        } catch (Exception e) {
            AjaxResponse ajaxResponse=new AjaxResponse();
            ajaxResponse.setCode(500);
            ajaxResponse.setMessage("登录异常");
            return ajaxResponse;
        }
    }

    @GetMapping("/user/{id}")
    public AjaxResponse getUser(@PathVariable(name = "id")int id){
        User user = userService.findById(id);
        AjaxResponse success = AjaxResponse.success(user);
        return success;
    }

    @PutMapping("/user")
    public AjaxResponse updateUser(@RequestBody User user){

        userService.updateUser(user);
        AjaxResponse success = AjaxResponse.success();
        success.setMessage("修改成功");
        return success;
    }
}
